package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.${capitalize};
import cn.wolfcode.luowowo.query.${capitalize}Query;
import cn.wolfcode.luowowo.repository.${capitalize}Repository;
import cn.wolfcode.luowowo.service.I${capitalize}Service;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ${capitalize}ServiceImpl implements I${capitalize}Service{

    @Autowired
    private ${capitalize}Repository ${uncapitalize}Repository;

    @Autowired
    private MongoTemplate template;

    @Override
    public ${capitalize} get(String id) {
        return ${uncapitalize}Repository.findById(id).orElse(null);
    }

    @Override
    public List<${capitalize}> listAll() {
        return ${uncapitalize}Repository.findAll();
    }

    @Override
    public void delete(String id) {
        ${uncapitalize}Repository.deleteById(id);
    }

    @Override
    public void save(${capitalize} ${uncapitalize}) {
        ${uncapitalize}.setId(null);
        ${uncapitalize}Repository.save(${uncapitalize});
    }

    @Override
    public void update(${capitalize} ${uncapitalize}) {
        ${uncapitalize}Repository.save(${uncapitalize});
    }

    @Override
    public Page<${capitalize}> query(${capitalize}Query qo) {
        //查询条件
        Query query = new Query();
        //高级查询条件
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
            Sort.Direction.DESC, "_id");
        Page<${capitalize}> page = DBHelper.query(template, ${capitalize}.class, query, pageable);
        return page;
    }

}
